package algorithm;

import java.util.Arrays;

/**
 * 快慢指针解题
 */
public class FastSlowIndex {

    // 去除重复元素
    public static int[] removeDuplicates(int[] nums) {
        int fast=0,slow=0;
        while (fast<nums.length) {
            if(nums[fast]!=nums[slow]){
                slow++;
                nums[slow]=nums[fast];
            }
            fast++;
        }
        return Arrays.copyOf(nums, slow+1);
    }

    // 把0移动到最后
    public static int[] moveZeroes(int[] nums) {
        int slow=0,fast=0;
        while(fast<nums.length){
            if(nums[fast]!=0){
                int tmp = nums[slow];
                nums[slow] = nums[fast];
                nums[fast] = tmp;
                slow++;
            }
            fast++;
        }
        return nums;
    }

    public static void main(String[] args) {
//        System.out.println(Arrays.toString(removeDuplicates(new int[]{0,0,1,1,1,2,2,3,4,4,5})));
        System.out.println(Arrays.toString(moveZeroes(new int[]{0,0,1,1,2,3,0,5})));
    }


}
